#include "CmdCenter.h"
#include "Constants.h"


#include <iostream>
#include <algorithm>
#include <cctype>
#include <log4cpp/Category.hh>

namespace CmdCenter {

log4cpp::Category& logger =
    log4cpp::Category::getInstance(std::string(SERVER_LOGGER));

CmdCenter::CmdCenter(ServerThread::ServerThread& server):_server(server){

}

void CmdCenter::operator()(){

  bool done= false;
  while(!done) {

    std::cout << "cmd> " ;
    std::string line;
    std::getline(std::cin, line);

    if (!processCmd(line)) {
      done = true;
    }
  }
}

bool CmdCenter::processCmd(std::string& cmd) {

  if (cmd.length() < 0)
    return true;

  // trim leading and trailing spaces

  //convert to upper case
  for_each(cmd.begin(), cmd.end(), [](char& in){ in = ::toupper(in); });


  if (cmd == "QUIT") {
    std::cout << "Bye !" <<std::endl;
    return false;
  }
  else if (cmd == "STATUS") {
    std::cout << "Total Msg Received: " <<  (_server.msgCount()) <<std::endl;
  }

  return true;
}

}
